<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/functions/password/bdd.php';
session_start();
$token = md5(uniqid());
try {
    $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
    $bdd = new PDO('mysql:host=localhost;dbname=origavion', 'origavion', get_bdd_password(), $pdo_options);
} catch (Exception $e) {
    die($e->getmessage());
}
$prep_token = $bdd->prepare('INSERT INTO nb_photos_tmp(token, nb) VALUES(?, 0)');
$prep_token->execute(array($token));
?>
<script type="text/javascript" src="/admin/scripts/fileuploader.js"></script>
<script type="text/javascript">
	var i=1;
	var j = new Array();
	var attributions = new Array();
				j[0]=1;
				attributions[0] = new Array();

	function scookie(name,value,days){
		var expire = new Date();
		expire.setDate(expire.getDate()+days);
		document.cookie = name+'='+escape(value)+';expires='+expire.toGMTString();
		return true;
	}

	function addetape(i){
		j[i]=1;
		attributions[i] = new Array();
		document.getElementById('etape.'+i).innerHTML += '<fieldset><legend>Etape n°'+i+'</legend><p><textarea name="etape_'+i+'" id="etape_'+i+'" rows="10" cols="50" placeholder="Saisissez ici le contenu de l\'étape N°'+i+'"></textarea></p><div id="photos.'+i+'"></div></fieldset><div id="etape.'+(i+1)+'"></div>';
		refreshphoto(adresse);
	}

	function refreshphoto(photos){
		var tableau = new String();
		for (var k in j){
			while (attributions[k].shift());
			tableau = '<table><tr>';
			for (var l in photos){
				l = Number(l);
				if(l%2 == 0){
					tableau += '</tr><tr>';
				}
				tableau += '<td><input type="checkbox" name="photo_'+k+'_'+(l+1)+'" id="photo.'+k+'.'+(l+1)+'" style="display:none;" onclick="if (this.checked) {changeopacity(1, \'photo.'+k+'.'+(l+1)+'\');attrnumber('+k+', '+(l+1)+', attributions, 1);}else{changeopacity(0, \'photo.'+k+'.'+(l+1)+'\');attrnumber('+k+', '+(l+1)+', attributions, 0);};"/><label for="photo.'+k+'.'+(l+1)+'"><figure style="opacity:inherit;"><img src="'+photos[l]+'" height="160" style="opacity:inherit;"/><figcaption id="figcaption_photo.'+k+'.'+(l+1)+'">n.c</figcaption></figure></label></td>';
				attributions[k].push(l+1);
			}
			tableau += '</tr></table>';
			document.getElementById('photos.'+k).innerHTML = tableau;
		}
	}

	function storefichier(nomfichier, id){
		$.ajax({
			url: 'traitement/transfert.php',
			type: 'POST',
			data: {file:nomfichier,token:'<?php echo $token; ?>'},
			success: function(msg){
										alert(msg);
				if (msg != 'erreur'){
					adresse[id] = msg;
					refreshphoto(adresse);
				} else {
					return false;
				}
			},
			error: function(msg){
				storefichier(nomfichier, id)
			}
		});
	}

	function changeopacity(statut, name){
		if (statut == 1){
			$($("label[for='"+name+"']")[0]).css('opacity', '0.5');
		} else {
			$($("label[for='"+name+"']")[0]).css('opacity', '1');
		}
	};

	function attrnumber(etape, photo, attributions, statut){
		if (statut == 1){
			document.getElementById('figcaption_photo.'+etape+'.'+photo).innerHTML = attributions[etape].shift();
			document.getElementById('photo.'+etape+'.'+photo).value = document.getElementById('figcaption_photo.'+etape+'.'+photo).innerHTML
		} else {
			document.getElementById('figcaption_photo.'+etape+'.'+photo).innerHTML = 'n.c';
			attributions[etape].unshift(document.getElementById('photo.'+etape+'.'+photo).value);
			document.getElementById('photo.'+etape+'.'+photo).value = 'n.c';
		}
	}
</script>
<div class="element_main form_upload">
	<form method="POST" action="traitement/ajout_etapes.php" id="ajout_avion">
		<p>
			<label for = "nom">Nom de l'avion : </label><br/>
			<input type = "text" name = "nom" id = "nom" />
		</p>
		<p>
			Description de l'avion : <br />
			<textarea name = "description" id = "description" rows="10" cols="50" placeholder="Entrez la description de l'avion ici."></textarea>
		</p>
		<div id="file-uploader">       
			<noscript>          
			<p>Activez JavaScript pour pouvoir envoyer des fichiers.</p>
			</noscript>         
		</div>
		<p>
			<label for = "categorie">Catégorie dans laquelle sera classé l'avion : </label><br/>
			<select name = "categorie" id = "categorie"> 
				<option value = "planeur">Planeur</option>
				<option value = "voltigeur">Voltigeur</option>
				<option value = "avion_bizarre">Avion Bizarre</option>
				<option value = "prototype">Prototype (A Améliorer)</option>
			</select>
		</p>
		<p>
			<label for = "difficulte">Difficulté de réalisation : </label><br/>
			<input type = "number" name = "difficulte" id = "difficulte" size="1" style="text-align:right"/>
			/5
		</p>
		<p>
			<label for = "stabilite">Stabilité : </label><br/>
			<input type = "number" name = "stabilite" id = "stabilite" size="1" style="text-align:right"/>
			/5
		</p>
		<p>
			<label for = "stab_roul">Stabilité au roulis : </label><br/>
			<input type = "number" name = "stab_roul" id = "stab_roul" size="1" style="text-align:right"/>
			/5
		</p>
		<p>
			<label for = "stab_tang">Stabilité au tangage : </label><br/>
			<input type = "number" name = "stab_tang" id = "stab_tang" size="1" style="text-align:right"/>
			/5
		</p>
		<p>
			<label for = "vitesse">Vitesse d'évolution : </label><br/>
			<select name = "vitesse" id = "vitesse">
				<option value = "Haute">Haute</option>
				<option value = "Moyenne">Moyenne</option>
				<option value = "Basse">Basse</option>
			</select>
		</p>
		<p>
			<label for = "decrochage">Vitesse de décrochage : </label><br/>
			<select name = "decrochage" id = "decrochage">
				<option value = "Haute">Haute</option>
				<option value = "Moyenne">Moyenne</option>
				<option value = "Basse">Basse</option>
			</select>
		</p>
		<p>
			Réglages supplémentaires : <br />
			<textarea name = "reglages" id = "reglages" rows="10" cols="50" placeholder="Les réglages qui optimisent le vol sont à renseigner ici."></textarea>
		</p>
		<fieldset>
			<legend>Photos Principales de l'avion</legend>
			<div id="photos.0"></div>
		</fieldset>
		<div id ="etapes">
			<div id="etape.1">
			</div>
		</div>
		<p>
			<input type="button" onclick="addetape(i++);" value="Ajouter une étape"/>
		</p>
		<p>
			<input type="button" value = "Valider" onclick="scookie('nb_etapes', i-1, 1);document.getElementById('ajout_avion').submit();" />
		</p>	
		<script>
			var adresse = new Array();
			var uploader = new qq.FileUploader({
				element: document.getElementById('file-uploader'),
				action: 'traitement/recv_ajax.php',
				debug: true,
				onComplete: function(id, fileName, responseJSON){
					var nomfichier = responseJSON.filename;
					storefichier(nomfichier, id);
				},
				maxConnections: 1
			});
			$(function(){
				$(':checkbox').change(function(){
					alert($(this).val());
				});
			});
		</script>
						<input type="hidden" name="token" value="<?php echo $token; ?>" />
	</form>
</div>
</html>